import Vue from 'vue'
import Vuex from 'vuex'
import Tool from '../common/js/tool.js'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        userData:'',
        hasLogin: false,
		addresses:"",//当前地址
		latitude:Tool.localStorage.get('latitude')?Tool.localStorage.get('latitude'):'',//经度
		longitude:Tool.localStorage.get('longitude')?Tool.localStorage.get('longitude'):'',//纬度
		userInfo:Tool.localStorage.get('userInfo')?JSON.parse(Tool.localStorage.get('userInfo')):'',//用户基本信息
		
    },
    mutations: {
		login(state, provider) {
			state.hasLogin = true;
			state.userData = provider;
			 //缓存用户登陆状态
			uni.setStorageSync('userData',provider) 
		},
		logout(state) {
			state.hasLogin = false;
			state.userData = {};
			uni.removeStorageSync('userData')
		},
		historyUser(state, provider) {
			//缓存为历史登录用户
			uni.setStorageSync('historyUser',provider);
		},
		//设置定位信息
		setPosition(state,info){
			state.latitude = info.latitude;
			state.longitude = info.longitude;
			Tool.localStorage.set('latitude',info.latitude);
			Tool.localStorage.set('longitude',info.longitude);
		},
		//设置用户信息
		setUserInfo(state,info){
			state.userInfo = JSON.stringify(info.data.model);
			Tool.localStorage.set('userInfo',JSON.stringify(info.data.model))
		},
    },
    actions:{
        setUserData:function(context,obj){
            context.commit('login',obj)
        },
		logout:function(context){
		    context.commit('logout')
		},
		setHistoryUser:function(context,obj){
		    context.commit('historyUser',obj)
		},
    }
})

export default store